import PowerUp from './PowerUp.js'

// 减速道具类
export default class SlowPowerUp extends PowerUp {
  constructor(x, y) {
    super(x, y, 'slow')
    this.duration = 8000 // 减速效果持续8秒
    this.slowFactor = 0.5 // 减速因子，敌机速度将减为原来的一半
    this.rotationSpeed = 0.02 // 缓慢旋转，表示时间减缓
  }

  loadImage() {
    // 加载减速道具图像
    this.image.src =
      ''
  }

  // 获取光晕颜色
  getGlowColor() {
    return `rgba(136, 136, 255, ${this.glowOpacity})`
  }

  apply(player, game) {
    super.apply(player, game)

    // 应用减速效果
    game.enemySlowFactor = this.slowFactor

    // 为所有当前敌机应用减速效果
    for (const enemy of game.enemies) {
      enemy.originalSpeed = enemy.originalSpeed || enemy.speed
      enemy.speed = enemy.originalSpeed * this.slowFactor
    }

    // 设置减速效果的持续时间
    if (game.slowTimer) {
      clearTimeout(game.slowTimer)
    }

    // 添加减速状态到游戏状态
    game.addStatusEffect({
      type: 'slow',
      duration: this.duration,
      startTime: Date.now(),
    })

    // 显示提示信息
    game.showMessage('时间减缓！敌机减速！', 2000)

    // 添加时间减缓视觉效果
    this.addTimeSlowEffect(game)

    // 设置定时器，在持续时间结束后恢复敌机速度
    game.slowTimer = setTimeout(() => {
      // 恢复敌机速度
      game.enemySlowFactor = 1

      for (const enemy of game.enemies) {
        if (enemy.originalSpeed) {
          enemy.speed = enemy.originalSpeed
        }
      }

      // 移除减速状态
      game.removeStatusEffect('slow')
      game.showMessage('减速效果已结束', 1500)
    }, this.duration)
  }

  // 添加时间减缓视觉效果
  addTimeSlowEffect(game) {
    // 创建时钟指针旋转效果
    const clockHand = {
      x: game.canvas.width / 2,
      y: game.canvas.height / 2,
      radius: 100,
      angle: 0,
      opacity: 0.7,
      duration: 2000, // 效果持续2秒
      startTime: Date.now(),
      update: function () {
        const elapsed = Date.now() - this.startTime
        if (elapsed > this.duration) {
          return false
        }

        this.angle += 0.02
        this.opacity = 0.7 * (1 - elapsed / this.duration)
        return true
      },
      render: function (ctx) {
        ctx.save()
        ctx.translate(this.x, this.y)

        // 绘制时钟外圈
        ctx.beginPath()
        ctx.arc(0, 0, this.radius, 0, Math.PI * 2)
        ctx.strokeStyle = `rgba(136, 136, 255, ${this.opacity * 0.5})`
        ctx.lineWidth = 5
        ctx.stroke()

        // 绘制时钟指针
        ctx.rotate(this.angle)
        ctx.beginPath()
        ctx.moveTo(0, 0)
        ctx.lineTo(0, -this.radius * 0.8)
        ctx.strokeStyle = `rgba(255, 255, 255, ${this.opacity})`
        ctx.lineWidth = 3
        ctx.stroke()

        ctx.restore()
      },
    }

    // 添加到游戏特效中
    game.addSpecialEffect(clockHand)

    // 添加蓝色波纹效果
    for (let i = 0; i < 3; i++) {
      setTimeout(() => {
        const wave = {
          x: game.canvas.width / 2,
          y: game.canvas.height / 2,
          radius: 0,
          maxRadius: Math.max(game.canvas.width, game.canvas.height) * 0.8,
          opacity: 0.5,
          update: function () {
            this.radius += 8
            this.opacity -= 0.01
            return this.opacity > 0 && this.radius < this.maxRadius
          },
          render: function (ctx) {
            ctx.beginPath()
            ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2)
            ctx.strokeStyle = `rgba(136, 136, 255, ${this.opacity})`
            ctx.lineWidth = 6
            ctx.stroke()
          },
        }

        // 添加到游戏特效中
        game.addSpecialEffect(wave)
      }, i * 300) // 每300毫秒添加一个波纹
    }
  }
}
